<?php

/**
 * image actions.
 *
 * @package    grand
 * @subpackage image
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class galleryActions extends sfActions
{
  
  public function executeIndex(sfWebRequest $request)
  {
      $this->pager = Doctrine::getTable('Album')->getPager(array('keyword'=>$request->getParameter('keyword')), $request->getParameter('page'));
  }
  
  public function executeImages(sfWebRequest $request)
  {
      $this->album = $album = Doctrine_Core::getTable('Album')->find($request->getParameter('albumId'));

      $this->pager = Doctrine::getTable('Image')->getPager($album->getId(), '', $request->getParameter('page'));
      
      $this->albumId = $album->getId();
  }

  public function executeLoadObjects(sfWebRequest $request)
  {
      $objects = array();
      $seasonId = $request->getParameter('seasonId');
      $objectType = $request->getParameter('objectType');

      $teamIds = Doctrine::getTable('BbSeasonTeams')->getTeamIds($seasonId);
      switch ($objectType)
      {
      	case 'match': $objects = Doctrine::getTable('BbMatch')->fetchArray(array('seasonId'=>$seasonId)); break;
      	case 'team': $objects = Doctrine::getTable('BbTeams')->fetchArray(array(), $teamIds); break;
      	case 'player': $objects = Doctrine::getTable('BbPlayers')->fetchArray(array(), $teamIds);break;
      }
      if (!sizeof($objects)) return sfView::NONE;

      $this->objects = $objects;
      $this->objectId = $request->getParameter('objectId') ? $request->getParameter('objectId') : 0;
  }
  
  
  public function executeNew(sfWebRequest $request)
  {
      $this->form = new AlbumForm(null, array());
  }

  public function executeNewImage(sfWebRequest $request)
  {
      $this->albumId = $albumId = $request->getParameter('albumId');
      $this->form = new ImageForm(null, array('albumId'=>$albumId));
  }

  public function executeCreate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST));
  
      $this->form = new AlbumForm();
  
      $this->processForm($request, $this->form);
  
      $this->setTemplate('new');
  }
  
  
  public function executeCreateImage(sfWebRequest $request)
  {    
      $this->forward404Unless($request->isMethod(sfRequest::POST));
  
      $params = $request->getParameter('image');
      $this->albumId = $params['album_id'];
      $this->form = new ImageForm(null, array('albumId'=>$params['album_id']));
  
      $this->processFormImage($request, $this->form);
  
      $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
      $this->forward404Unless($image = Doctrine_Core::getTable('Album')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      $this->form = new AlbumForm($image);
  }
  
  public function executeEditImage(sfWebRequest $request)
  {
      $this->forward404Unless($image = Doctrine_Core::getTable('Image')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      $this->form = new ImageForm($image, array('albumId'=>$image->getAlbumId()));
      $this->albumId = $image->getAlbumId();
  }

  public function executeUpdate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
      $this->forward404Unless($image = Doctrine_Core::getTable('Album')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      $this->form = new AlbumForm($image);
  
      $this->processForm($request, $this->form);
  
      $this->setTemplate('edit');
  }
  
  public function executeUpdateImage(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
      $this->forward404Unless($image = Doctrine_Core::getTable('Image')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      $this->form = new ImageForm($image, array('albumId'=>$image->getAlbumId()));
  
      $this->processFormImage($request, $this->form);
      
      $this->albumId = $image->getAlbumId();
      $this->setTemplate('edit');
  }


  public function executeDelete(sfWebRequest $request)
  {
      $this->forward404Unless($album = Doctrine_Core::getTable('Album')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      
      $images = Doctrine_Core::getTable('Album')->findBy('id', $album->getId());
      $objectType = $album->getObjectType();
      /*if(sizeof($images)) 
      {
        $this->getUser()->setFlash('success', 'Цомгийн зургуудыг усгтгасны дараа цомгийг устгах боломжтой.', true);
      }else {*/
        $album->delete();  
      /*}*/
      $this->redirect('gallery/index?objectType='.$objectType);
  }

  public function executeDeleteImage(sfWebRequest $request)
  {
      $this->forward404Unless($image = Doctrine_Core::getTable('Image')->find(array($request->getParameter('id'))), sprintf('Object image does not exist (%s).', $request->getParameter('id')));
      $albumId = $image->getAlbumId();
      $image->delete();
  
      $this->redirect('gallery/images?albumId='.$albumId);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
      if ($form->isValid())
      {
        $album = $form->save();
  
        $this->getUser()->setFlash('success', 'Цомог амжилттай нэмэгдлээ. Шинэ цомогтоо зураг нэмээрэй.', true);
        
        $this->redirect('gallery/newImage?albumId='.$album->getId());
      }
  }
  

  protected function processFormImage(sfWebRequest $request, sfForm $form)
  {
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
      if ($form->isValid())
      {
        $image = $form->save();
        
        //myTools::resizeImages($image->getFilename(), 'gallery', array(120));
        
        $this->getUser()->setFlash('success', 'Зураг амжилттай хадгалагдлаа. Та цомгийн зургууд линк дээр дарж оруулсан зургуудаа харах боломжтой.', true);
  
        $this->redirect('gallery/newImage?albumId='.$image->getAlbumId());
      }
  }

  
}